<?php
    //Load all php file
    include("ClassLoader/Loader.php");
   // ob_start(); //Begin traking data
    session_start();
    $loader = new Loader();
    $loader->load();
    
    //global var
    if ($_SESSION['userid'])
        $curUser = User::withId($_SESSION['userid']);

$title = 'ySocial - A photo social network';
$mainClass = 'main-full';
$topWidget = '';
$header = '';
$images = '';
$baseurl = Configuration::$baseUrl;
//Param parser
#remove the directory path we don't want 
$request = urldecode(substr($_SERVER['REQUEST_URI'], strlen($baseurl)));
if (strpos($request, '?') != '') {
    $request = substr($request, 0, strpos($request, "?"));
}
#split the path by '/'
$params = split("/", $request);
$streamId = 1;

switch (strtoupper($params[0])) {
    case "M":
        //$soapRequest = $_REQUEST['request'];
        $server = new bpServer($baseurl . "m/");
        //var_dump($server);
        $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';
        $server->service($HTTP_RAW_POST_DATA);
        return;
        break;
    case "SIGNOUT":
        $_SESSION['userid'] = null;
        $params[0] = "HOME";
    case "":
    case "HOME":
        $params[0] = "shots";
        $params[1] = "popular";
    case "SHOTS":
        if (!strpos($params[1], '-')) {
            if (!($params[1]))
                $params[1] = 'following';
            if (!isset($_REQUEST['page']))
                $_REQUEST['page'] = 1;
            $photoController = new PhotoController(null, new ShotsView());
            $photoController->render($params[1], $_REQUEST['page'], $_REQUEST['date']);
            $params[1] = 'Grid';
        }
        else {
            $photoParams = explode('-', $params[1]);
            $photo = Photo::withId($photoParams[0]);
            $photoController = new PhotoController($photo, new ShotsView());
            $photoController->displayPhoto($params[1]);
            $params[1] = 'Detail';
            $secondary = "Detail/ShotsSecondaryHTML.php";
        }
        break;
    case "COLORS":
        if (!($params[1]))
            $params[1] = '660000';
        if (!isset($_REQUEST['page']))
            $_REQUEST['page'] = 1;
        $colorController = new ColorController(null, new ColorsView());
        $colorController->render($params[1], $_REQUEST['page']);
        $params[1] = 'Grid';
        $secondary = "Grid/ColorsSecondaryHTML.php";
        $mainClass = '';
        break;
    case "EXPLORE":
        if (sizeof($params) == 2) {
            switch (strtoupper($params[1])) {
                case "HIGHLIGHTS":
                    break;
                case "COLORS":
                    $params[0] = 'colors';
                    $params[1] = '660000';
                    if (!($params[1]))
                        $params[1] = '660000';
                    if (!isset($_REQUEST['page']))
                        $_REQUEST['page'] = 1;
                    $colorController = new ColorController(null, new ColorsView());
                    $colorController->render($params[1], $_REQUEST['page']);
                    $params[1] = 'Grid';
                    $secondary = "Grid/ColorsSecondaryHTML.php";
                    $mainClass = '';
                    break;
            }
        }
        else {
            $bottomModule = 'BottomModule.php';
        }
        break;
    case "ABOUT":
        $hero = "AboutHeroHTML.php";
        $secondary = "AboutSecondaryHTML.php";
        $bottomModule = 'BottomModule.php';
        $mainClass = 'site';
        break;
    case "AJAX":
        if (file_exists("View/Master/$params[1]HTML.php"))
            include "View/Master/$params[1]HTML.php";
        return;
        break;
    case "USER":
        break;
    case "LOGIN":
        $userView = UserView::getInstance();
        $userView->display("LoginViewHTML.php");
        $title = "Login to buzzPulse";
        break;
    case "SIGNUP":
        $hero = "SignupHeroHTML.php";
        $secondary = "SignupSecondaryHTML.php";
        $bottomModule = 'BottomModule.php';
        $mainClass = '';
        $topWidget = 'SignupTopWidget.php';
        break;
    case "TEST":
        foreach (Color::getFromImage($_REQUEST['url']) as $color):
            echo "<div style='width: 50px; height: 50px; display: inline-block; margin: 2px; background: #$color;'></div>";
        endforeach;
        return;
    case "ACCOUNT":
        if (sizeof($params) == 2) {
            switch (strtoupper($params[1])) {
                case "DESTROYCONFIRM":
                    $secondary = null;
                    $topWidget = $params[1] . "/DestroyConfirmTopWidget.php";
                    $bottomModule = $params[1] . "/BottomModule.php";
                    break;
                case "PASSWORD":
                    $secondary = null;
                    $topWidget = $params[1] . "/PasswordTopWidget.php";
                    $bottomModule = $params[1] . "/BottomModule.php";
                    break;
                case "NOTIFICATIONS":
                    $secondary = $params[1] . "/NotificationsSecondaryHTML.php";
                    $topWidget = $params[1] . "/NotificationsTopWidget.php";
                    $bottomModule = $params[1] . "/BottomModule.php";
                    break;
            }
        } else {
            $secondary = "AccountSecondaryHTML.php";
            $topWidget = "AccountTopWidget.php";
            $bottomModule = 'BottomModule.php';
        }
        break;
    case "AUTHORIZETWITTER":
        if (!isset($_GET["oauth_token"])) {
            Twitter::getAuthorization($_SERVER['HTTP_HOST'] . '/authorizeTwitter');
        } else {
            $twitterid = Twitter::getCredentials();
            if ($twitterid != 'false') {
                $response = Twitter::getUserInfo($twitterid);
                $_SESSION['twitter'] = $response;
                echo "<script>window.close()</script>";
            }
        }
        return;
    case "UPLOAD":
        if ($_POST['photo']) {
            $topWidget = 'UploadTopWidget.php';
        }
        break;
    default:
        $params[1] = $params[0];
        $params[0] = "Profile";
        $bottomModule = 'BottomModule.php';
        $profile = "ProfileActionHTML.php";
        $secondary = "ProfileSecondaryHTML.php";
        $mainClass = '';
        $user = User::withEmail($params[1]);
        $title = 'ySocial - ' . $user->name;
        $profileControler = new ProfileController($user, new stdClass());
        if (!$_REQUEST['page'])
            $_REQUEST['page'] = 1;
        $profileControler->render($user, $_REQUEST['page'], 12, $params[2]);
        break;
}
$params[0] = ucfirst($params[0]);
if (sizeof($params) == 2) {
    $params[1] = ucfirst($params[1]);
}
if (strtoupper($params[0]) !== "LOGIN") {
    //Generate View
    $masterView = MasterView::getInstance();
    if ($images) {
        $masterView->mainView = "View/$params[0]/" . $params[0] . "ViewHTML.php";
    } else {
        if(sizeof($params) == 2 && strtoupper($params[0]) != "PROFILE" && strtoupper($params[0]) != "UPLOAD" && strtoupper($params[0]) != "ABOUT"){
            $masterView->mainView = "View/$params[0]/$params[1]/" . $params[1] . "ViewHTML.php";
        } else {
            $masterView->mainView = "View/$params[0]/" . $params[0] . "ViewHTML.php";
        }
    }
    if ($header)
        $masterView->header = "View/$params[0]/$header";
    if ($topWidget)
        $masterView->topWidget = "View/$params[0]/$topWidget";
    if ($hero)
        $masterView->hero = "View/$params[0]/$hero";
    if ($secondary)
        $masterView->secondary = "View/$params[0]/$secondary";
    if ($profile)
        $masterView->profile = "View/$params[0]/$profile";
    $masterView->title = $title;
    $masterView->mainClass = $mainClass;
    if ($bottomModule)
        $masterView->bottomModule = "View/$params[0]/$bottomModule";
    $masterView->params = $params;
    $masterView->baseUrl = $baseurl;
    $masterView->images = $images;
    $masterView->display("MasterPage.php");
}

function Login($email, $password) {
    return bpServer::Login($email, $password);
}

function deleteStatus($statusid) {
    return bpServer::deleteStatus($statusid);
}

function deletePhotoWS($photoid) {
    return bpServer::deletePhotoWS($photoid);
}

function deletePhotoData($photoid) {
    return bpServer::deletePhotoData($photoid);
}

function addFollow($userid1, $userid2, $datecreated) {
    return bpServer::addFollow($userid1, $userid2, $datecreated);
}

function checkFollow($userid1, $userid2) {
    return bpServer::checkFollow($userid1, $userid2);
}

function deleteFollow($userid1, $userid2) {
    return bpServer::deleteFollow($userid1, $userid2);
}

function getColorBar($url) {
    return bpServer::getColorBar($url);
}

function addLike($statusid, $userid, $datecreated) {
    return bpServer::addLike($statusid, $userid, $datecreated);
}

function deleteLike($statusid, $userid) {
    return bpServer::deleteLike($statusid, $userid);
}

function getAmountLike($statusid) {
    return bpServer::getAmountLike($statusid);
}

?>
